package com.yehuishou.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yehuishou.dao.FileDao;
import com.yehuishou.pojo.Address;
import com.yehuishou.pojo.Erwm;
import com.yehuishou.pojo.Identity;
import com.yehuishou.pojo.ImageFile;
import com.yehuishou.pojo.User;
import com.yehuishou.service.UserService;

/**
 * 用户管理
 * 
 * @author zxb
 *
 */
@Controller
public class UserController {

	@Autowired
	private UserService userService;
	@Autowired
	private FileDao fileDao;

	@RequestMapping("userlist")
	public String userList(@RequestParam(required = true, defaultValue = "1") Integer pageNum,
			@RequestParam(required = true, defaultValue = "10") Integer pageSize, Model model) {
		PageHelper.startPage(pageNum, pageSize);
		List<User> list = userService.findAll();
		PageInfo<User> p = new PageInfo<User>(list);
		model.addAttribute("page", p);
		model.addAttribute("list", list);
		return "content-7_2";
	}

	@RequestMapping("userfind")
	public String userfind(@RequestParam(required = true, defaultValue = "1") Integer pageNum,
			@RequestParam(required = true, defaultValue = "10") Integer pageSize, String phone, Integer state,
			Model model) {
		PageHelper.startPage(pageNum, pageSize);
		List<User> list = userService.findBy(phone, state);
		PageInfo<User> p = new PageInfo<User>(list);
		List<Object> fan = new ArrayList<Object>();
		fan.add(phone);
		fan.add(state);
		model.addAttribute("page", p);
		model.addAttribute("list", list);
		model.addAttribute("fan", fan);
		return "content-7_2";
	}

	@RequestMapping("userxq")
	public String userxq(Integer uid, Model model) {
		User user = userService.findById(uid);
		Address ads = userService.findAds(uid);
		Identity ide = userService.findIde(uid);
		if (ide != null) {
			String img = ide.getImage();
			if (img != null && img != "") {
				String[] imgid = img.split(";");
				List<Integer> ids = new ArrayList<Integer>();
				for (int i = 1; i < imgid.length; i++) {
					if (imgid[i] != null) {
						ids.add(Integer.valueOf(imgid[i]));
					}
				}
				List<ImageFile> imageid = fileDao.getImageByid(ids);
				model.addAttribute("imageid", imageid);
			}
		}
		model.addAttribute("user", user);
		model.addAttribute("ads", ads);
		model.addAttribute("ide", ide);
		return "userxq";
	}

	@RequestMapping("usersh")
	public String usersh(Integer uid, Integer state, Integer istate) {
		userService.updateU(uid, state);
		userService.updateI(uid, istate);
		return "redirect:/userlist";
	}

	// 用户审核
	@RequestMapping("userWSH")
	public String userWSH(@RequestParam(required = true, defaultValue = "1") Integer pageNum,
			@RequestParam(required = true, defaultValue = "10") Integer pageSize, String phone, Model model) {
		PageHelper.startPage(pageNum, pageSize);
		List<User> list = userService.findByW(phone);
		PageInfo<User> p = new PageInfo<User>(list);
		model.addAttribute("page", p);
		model.addAttribute("list", list);
		model.addAttribute("phone", phone);
		return "userWSH";
	}

	@RequestMapping("userWsh")
	public String userWsh(Integer uid, Integer state, Integer istate) {
		userService.updateU(uid, state);
		userService.updateI(uid, istate);
		return "redirect:/userWSH";
	}

	// 删除用户
	@ResponseBody
	@RequestMapping("userdel")
	public Boolean delu(Integer uid) {
		userService.del(uid);
		return true;
	}
	
	//查询二维码
	@RequestMapping("erwm")
	public String erwm(Integer uid , Model model) {
		Erwm erwm = userService.findErwm(uid);
		model.addAttribute("erwm", erwm);
		return "erwm";
	}
}
